package cn.tedu.javagame.FirePlane;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class Hero {
    int x, y;
    int life, magic, score, gold;
    int fire = 3; // 1表示单倍火力,2表示双倍火力
    Image  heroImg;
    List<Image> heroImgs = new ArrayList<Image>();

    public Hero() {
        this.x = 200;
        this.y = 500;
        this.life = 100;
        this.magic = 100;
        this.score = 0;
        this.gold = 0;
        this.heroImg = new ImageIcon("plane/ws00.png").getImage();
        // 创建英雄的图片
        for(int i = 1; i <= 9; i++){
            // 加载图片
            heroImgs.add(new ImageIcon("plane/ws0" + i + ".png").getImage());
        }
    }

    public void move(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public List<Bullet> shoot() {
        // 根据火力值来发射子弹
        /**首先创建一个集合
         * 根据火力值，创建一个或多个子弹，添加到集合
         * 最后返回这个集合
         **/
        List<Bullet> bullets = new ArrayList<>();
        if (fire == 1) {
            // 找到英雄机的 嘴巴 的位置坐标
            int x = this.x + this.heroImg.getWidth(null) / 2;
            int y = this.y;
            Bullet bullet = new Bullet(x, y);
            bullets.add(bullet);
        }
        if (fire == 2) {
            int x1 = this.x;
            int y1 = this.y;
            int x2 = this.x + this.heroImg.getWidth(null);
            int y2 = this.y;
            Bullet bullet1 = new Bullet(x1, y1);
            Bullet bullet2 = new Bullet(x2, y2);
            bullets.add(bullet1);
            bullets.add(bullet2);
        }
        if (fire == 3) {
            int x1 = this.x;
            int y1 = this.y;
            int x2 = this.x + this.heroImg.getWidth(null) / 2;
            int y2 = this.y;
            int x3 = this.x + this.heroImg.getWidth(null);
            int y3 = this.y;
            Bullet bullet1 = new Bullet(x1, y1);
            Bullet bullet2 = new Bullet(x2, y2);
            Bullet bullet3 = new Bullet(x3, y3);
            bullets.add(bullet1);
            bullets.add(bullet2);
            bullets.add(bullet3);
        }
        return bullets;
    }

    // 碰到敌机减少生命
    public boolean hitEnemy(Enemy e) {
        // 简单的碰撞检测：判断英雄机的矩形区域是否与敌机的矩形区域相交
        Rectangle heroRect = new Rectangle(x, y, heroImg.getWidth(null), heroImg.getHeight(null));
        Rectangle enemyRect = new Rectangle(e.x, e.y, e.enemyImg.getWidth(null), e.enemyImg.getHeight(null));
        return heroRect.intersects(enemyRect);
    }

    public boolean hitBullet(Bullet bullet) {
        Rectangle heroRect = new Rectangle(x, y, heroImg.getWidth(null), heroImg.getHeight(null));
        Rectangle bulletRect = new Rectangle(bullet.x, bullet.y, bullet.bulletImg.getWidth(null), bullet.bulletImg.getHeight(null));
        return heroRect.intersects(bulletRect);
    }

    // 碰到红心增加生命
    public void hitHeart(Heart heart) {
        heart.increaseHeroLife(this);
    }

    public void hitBoss(Boss boss) {}
}